package cn.mumzone.pos.ui.fragment.member;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.base.BaseFragment;
import cn.mumzone.basic.net.response.PageListResponse;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.PosToast;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.pos.R;
import cn.mumzone.pos.event.TimeCardNumEvent;
import cn.mumzone.pos.model.TimeCardEntity;
import cn.mumzone.pos.presenter.TimesCardPresenter;
import cn.mumzone.pos.presenter.contract.TimesCardContract;
import cn.mumzone.pos.ui.activity.ACT_SalesCard;
import cn.mumzone.pos.ui.adapter.ADA_MemberTimesCard;
import cn.mumzone.pos.ui.widgets.others.StoreHouseHeader;
import cn.mumzone.pos.ui.widgets.refresh.SmartRefreshLayout;
import cn.mumzone.pos.ui.widgets.refresh.api.RefreshLayout;
import cn.mumzone.pos.ui.widgets.refresh.listener.OnRefreshListener;

public class FRA_TimesCard extends BaseFragment<TimesCardPresenter> implements TimesCardContract.View {
    @BindView(R.id.rlv_times_card_available)
    RecyclerView rlvTimesCardAvailable;
    @BindView(R.id.rlv_times_card_unavailable)
    RecyclerView rlvTimesCardUnavailable;

    private String memberId;

    private String memberPhone;
    @BindView(R.id.smart_refresh_layout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.no_times_card_layout)
    LinearLayout noTimesCardLayout;

    @BindView(R.id.ns_have_times_card)
    NestedScrollView nsHaveTimesCard;

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        memberId = extras.getString("memberId");
        memberPhone = extras.getString("memberPhone");
    }

    @Override
    protected void onUserVisible() {
    }

    @Override
    protected void onFirstUserVisible() {
        super.onFirstUserVisible();
        smartRefreshLayout.autoRefresh();
    }

    @Override
    protected void initEventAndData() {
        mPresenter.initEvent();
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getTimesCardAllListPage(memberId, String.valueOf(2), String.valueOf(1),
                        String.valueOf(Integer.MAX_VALUE - 1));
            }
        });
    }

    @Override
    protected int getLayout() {
        return R.layout.fra_times_card;
    }

    @Override
    public void setTimesCardData(PageListResponse<TimeCardEntity> timesCard) {
        if (timesCard == null || timesCard.getList() == null || timesCard.getList().isEmpty()) {
            noTimesCardLayout.setVisibility(View.VISIBLE);
            nsHaveTimesCard.setVisibility(View.GONE);
        } else {
            noTimesCardLayout.setVisibility(View.GONE);
            nsHaveTimesCard.setVisibility(View.VISIBLE);

            //可用次卡
            rlvTimesCardAvailable.setLayoutManager(new GridLayoutManager(getActivity(), 3) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            });
            ADA_MemberTimesCard availableTimesCardAdapter = new ADA_MemberTimesCard(getActivity(), memberPhone);
            rlvTimesCardAvailable.setAdapter(availableTimesCardAdapter);
            List<TimeCardEntity> availableTimesCard = new ArrayList<>();
            List<TimeCardEntity> unAvailableTimesCard = new ArrayList<>();
            for (int i = 0; i < timesCard.getList().size(); i++) {
                if (timesCard.getList().get(i).getStatus() == 0) {
                    availableTimesCard.add(timesCard.getList().get(i));
                } else {
                    unAvailableTimesCard.add(timesCard.getList().get(i));
                }
            }
            availableTimesCardAdapter.setDataList(availableTimesCard);

            RxBus.getDefault().post(new TimeCardNumEvent(String.valueOf(availableTimesCard.size())));
            //让会员详情那个Activity刷新个数

            //不可用次卡
            rlvTimesCardUnavailable.setLayoutManager(new GridLayoutManager(getActivity(), 3) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            });
            ADA_MemberTimesCard unAvailableTimesCardAdapter = new ADA_MemberTimesCard(getActivity(), memberPhone);
            rlvTimesCardUnavailable.setAdapter(unAvailableTimesCardAdapter);
            unAvailableTimesCardAdapter.setDataList(unAvailableTimesCard);
        }
    }

    @Override
    public void dismissLoading() {
        smartRefreshLayout.finishRefresh();
    }

    @Override
    public void refreshData() {
        smartRefreshLayout.autoRefresh();
    }

    @OnClick({R.id.tv_add_time_card, R.id.tv_add_times_card})
    public void toggleEvent(View view) {
        switch (view.getId()) {
            case R.id.tv_add_time_card:
            case R.id.tv_add_times_card:
                Intent intent = new Intent(getActivity(), ACT_SalesCard.class);
                Bundle bundle = new Bundle();
                bundle.putString("memberPhone", memberPhone);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
        }
    }

    @Override
    protected View getTargetView() {
        return smartRefreshLayout;
    }

    @Override
    protected void refreshNetwork() {
        smartRefreshLayout.autoRefresh();
    }

    @Override
    public void showNetError() {
        smartRefreshLayout.finishRefresh();

        CommonAdapter availableAdapter = (CommonAdapter) rlvTimesCardAvailable.getAdapter();
        CommonAdapter unavailableAdapter = (CommonAdapter) rlvTimesCardUnavailable.getAdapter();

        if (null != availableAdapter && null != unavailableAdapter && ListUtil.isListEmpty(availableAdapter.getDataList())
                && ListUtil.isListEmpty(unavailableAdapter.getDataList())) {
            super.showNetError();
        } else {
            PosToast.INSTANCE.makeShortToast(mContext, "网络不给力，请稍后重试");
        }
    }

}
